package cn.oms.manage.controller;

import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.web.JsonResult;
import cn.oms.manage.pojo.param.CustomerAddNewParam;
import cn.oms.manage.pojo.param.MedicineUpdateParam;
import cn.oms.manage.pojo.param.RecordAddNewParam;
import cn.oms.manage.pojo.param.RecordUpdateParam;
import cn.oms.manage.pojo.vo.MedicineListVO;
import cn.oms.manage.pojo.vo.MedicineStandardVO;
import cn.oms.manage.pojo.vo.RecordListVO;
import cn.oms.manage.pojo.vo.RecordStandardVO;
import cn.oms.manage.service.IRecordService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 说明：
 *
 * @author:Hyx
 * @date:2023/6/16
 **/
@Slf4j
@RestController
@RequestMapping("/records")
@Api(tags = "05.诊断管理模块")
@Validated
public class RecordController {
    @Autowired
    private IRecordService recordService;

    public RecordController(){
    }

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/records/add-new')")
    @ApiOperation("添加诊断记录")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid RecordAddNewParam recordAddNewParam){
        log.debug("开始处理【添加诊断记录】的请求，参数：{}", recordAddNewParam);
        recordService.addNew(recordAddNewParam);
        return JsonResult.ok();

    }

    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/records/delete')")
    @ApiOperation("根据ID删除诊断记录")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "诊断ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的诊断ID值！") Long id) {
        log.debug("开始处理【根据ID删除诊断记录】的请求，参数：{}", id);
        recordService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/records/update')")
    @ApiOperation("修改诊断详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "诊断记录ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的诊断ID值！") Long id,
                                           @Valid RecordUpdateParam recordUpdateParam) {
        log.debug("开始处理【修改诊断记录详情】的请求，ID：{}，新数据：{}", id, recordUpdateParam);
        recordService.updateInfoById(id, recordUpdateParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}/renew")
    @PreAuthorize("hasAuthority('/records/read')")
    @ApiOperation("根据ID查询刷新详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "诊断ID", required = true, dataType = "long")
    })
    public JsonResult<Void> renew(
            @PathVariable @Range(min = 1, message = "请提交有效的药品ID值！") Long id) {
        log.debug("开始处理【根据ID刷新诊断详情】的请求，参数：{}", id);
        recordService.renew(id);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/records/read')")
    @ApiOperation("根据ID查询诊断详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "诊断ID", required = true, dataType = "long")
    })
    public JsonResult<RecordStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的药品ID值！") Long id) {
        log.debug("开始处理【根据ID查询诊断详情】的请求，参数：{}", id);
        RecordStandardVO queryResult = recordService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }



    @GetMapping("")
    @PreAuthorize("hasAuthority('/records/read')")
    @ApiOperation("查询诊断列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult<PageData<RecordListVO>> list(@Range(min = 1, message = "请提交有效的页码值！") Integer page,
                                                   String queryType) {
        log.debug("开始处理【查询药品列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<RecordListVO> pageData;
        if ("all".equals(queryType)) {
            pageData = recordService.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = recordService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }
}
